package com.guohao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guohao.reggie.common.R;
import com.guohao.reggie.dto.DishDto;
import com.guohao.reggie.entity.Category;
import com.guohao.reggie.entity.Dish;
import com.guohao.reggie.entity.DishFlavor;
import com.guohao.reggie.service.CategoryService;
import com.guohao.reggie.service.DishFlavorService;
import com.guohao.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 菜品 管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;//菜品

    @Autowired
    private DishFlavorService dishFlavorService; //菜品口味

    @Autowired
    private CategoryService categoryService; //分类


    /**
     * 新增 菜品
     * @param dishDto --
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info("dishDto {}:",dishDto.toString());

        dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }

    /**
     * 查询分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize , String name){
        // 构建分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);

        Page<DishDto> dishDtoPage = new Page<>(page,pageSize);
        // 条件构造器
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 添加过滤条件
        dishLambdaQueryWrapper.like(name != null,Dish::getName,name);

        // 添加排序条件
        dishLambdaQueryWrapper.orderByDesc(Dish::getCreateTime);

        // MP 查询
        dishService.page(pageInfo,dishLambdaQueryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();
        /**
         * 通过stream流 拿到 Dish数据
         * 获取分类id
         * 通过id查询分类对象整体数据 拿到分类名称 并且复制给dishDto对象
         * 把其他值 拷贝到 dishDto对象中
         * 这样dishDto对象 就有了Dish值 和 分类名称
         */
        List<DishDto> list = records.stream().map((item)->{
            DishDto dishDto = new DishDto();
            //对象拷贝
            BeanUtils.copyProperties(item,dishDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            // 查到拿category对象的Name 赋值给dishDto的CategoryName
            if (category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }

            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);

    }

    /**
     * 根据id 查询菜品信息和对应的分类信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){

        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return R.success(dishDto);
    }

    /**
     * 修改 菜品
     * @param dishDto --
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info("dishDto {}:",dishDto.toString());

        dishService.updateWithFlavor(dishDto);

        return R.success("更新套餐成功");
    }

    /**
     * 根据Id进行删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(@RequestParam List<Long> ids){
        log.info("删除分类 : id位:{}", ids.toString());

        //调用 重写remove方法 (Service 重写)
        dishService.remove(ids);
        return R.success("删除菜品成功!");
    }


    /**
     * 更新 禁卖 状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){

        // 创建 UpdateWrapper
        LambdaUpdateWrapper<Dish> dishLambdaUpdateWrapper = new LambdaUpdateWrapper<>();

        dishLambdaUpdateWrapper.set(Dish::getStatus,status);

        dishLambdaUpdateWrapper.in(Dish::getId,ids);

        dishService.update(dishLambdaUpdateWrapper);

        return R.success("状态更新成功!");
    }

    /**
     * 根据条件 查询对应的菜品数据
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        //1.构建条件构造器
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //2.添加查询条件
        dishLambdaQueryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        //查询 启售  禁售不查
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);

        //3. 添加排序条件
        dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        //4. MP 查询
        List<Dish> list = dishService.list(dishLambdaQueryWrapper);

        List<DishDto> dishDtoList = list.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();// 分类ID

            Category category = categoryService.getById(categoryId);
            if (category != null){
                String name = category.getName();
                dishDto.setCategoryName(name);
            }

            Long dishID = item.getId();//菜品ID
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishID);

            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(dishFlavorList);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishDtoList);
    }


//    public R<List<Dish>> list(Dish dish){
//        //1.构建条件构造器
//        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
//
//        //2.添加查询条件
//        dishLambdaQueryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
//        //查询 启售  禁售不查
//        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
//
//        //3. 添加排序条件
//        dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//
//        //4. MP 查询
//        List<Dish> list = dishService.list(dishLambdaQueryWrapper);
//        return R.success(list);
//    }

}
